package hotel.controller.employee;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import hotel.common.MsgBox;
import hotel.dao.HotelDao;
import hotel.modal.Employee;
import javafx.event.ActionEvent;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ChoiceBox;

public class EmployeeAddController {
	@FXML private TextField phoneField;
	@FXML private TextField nameField;
	@FXML private TextField employeeIdField;
	@FXML private TextField certIdField;
	@FXML private TextField addressField;
	@FXML private DatePicker entryTimeDp;
	@FXML private DatePicker birthdayDp;
	@FXML private ChoiceBox<String> genderCh;
	@FXML private ChoiceBox<String> statusCh;
	@FXML private Button btnOk;
	@FXML private Button btnCancel;
	Employee employee = null;

	@FXML
    public void initialize() {
		
	}
	
	public void initData(Employee employee) {
		this.employee = employee;
		employee.setEmployeeId(HotelDao.getNextEmployeeId());
		employeeIdField.setText(String.valueOf(employee.getEmployeeId()));
		DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
		Date d = new Date();
		String now = sdf.format(d);
		entryTimeDp.setValue(LocalDate.parse(now, dateFormatter));
	}
	
	// Event Listener on Button[#btnOk].onAction
	@FXML
	public void onBtnOkAction(ActionEvent event) {
		Boolean pass = true;		// 检查标志
		String name = nameField.getText();
		String certId = certIdField.getText();
		LocalDate birthday = birthdayDp.getValue();
		LocalDate entryTime = entryTimeDp.getValue();
		
		if(pass && name.equals("")) {
			pass = false;
			MsgBox.showErrorDialog("错误", null, "姓名不能为空！");
			nameField.requestFocus();
		}
		
		if(pass) {
			if(certId.equals("")) {
				pass = false;
				MsgBox.showErrorDialog("错误", null, "身份证号不能为空！");
				certIdField.requestFocus();
			}else if(certId.length() != 18) {
				pass = false;
				MsgBox.showErrorDialog("错误", "身份证号长度错误", "请检查是否为18位！");
				certIdField.requestFocus();
				certIdField.selectAll();
			}
		}
		
		if(pass && birthday == null) {
			pass = false;
			MsgBox.showErrorDialog("错误", null, "请正确填写出生日期！");
			birthdayDp.requestFocus();
		}
		
		if(pass && birthday == null) {
			pass = false;
			MsgBox.showErrorDialog("错误", null, "请正确填写入职日期！");
			birthdayDp.requestFocus();
		}
		
		if(pass) {
			employee.setName(name);
			employee.setCertId(certId.toUpperCase()); // 身份证号转大写
			employee.setPhone(phoneField.getText().trim());
			employee.setAddress(addressField.getText().trim());
			employee.setGender(genderCh.getSelectionModel().getSelectedItem().equals("男")?"M":"F");
			employee.setStatus(statusCh.getSelectionModel().getSelectedItem().equals("在职")?true:false);
			DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
			employee.setBirthday(dateFormatter.format(birthday));
			employee.setEntryTime(dateFormatter.format(entryTime));
			employee.setModified(true);
			
			Stage stage = (Stage) btnCancel.getScene().getWindow();
			stage.hide();
			MsgBox.showInformationDialog("添加成功", null, "员工("+ employee.getName() +")已经添加成功！");
			
			closeWindow();
		}else {
			pass = true;
		}
	}
	
	// Event Listener on Button[#btnCancel].onAction
	@FXML
	public void onBtnCancelAction(ActionEvent event) {
		employee.setModified(false);
		closeWindow();
	}
	
	private void closeWindow() {
		Stage stage = (Stage) btnCancel.getScene().getWindow();
		stage.close();
	}
	
}
